package com.itheima.controller;


import com.alibaba.druid.util.Utils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.CustomException;
import com.itheima.common.R;
import com.itheima.dto.SetmealDto;
import com.itheima.pojo.Category;
import com.itheima.pojo.Dish;
import com.itheima.pojo.Setmeal;
import com.itheima.pojo.SetmealDish;
import com.itheima.service.CategoryService;
import com.itheima.service.SetmealDishService;
import com.itheima.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 套餐功能
 */
@Slf4j
@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private CategoryService categoryService;

    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto){
        setmealService.saveWithDish(setmealDto);
        return R.success("成功添加");
    }

    @GetMapping("/page")
    public R<Page> page(int page,int pageSize,String name){
        Page<Setmeal> pageInfo = new Page<>(page,pageSize);
        Page<SetmealDto> pageInfo2 = new Page<>();

        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        qw.like(name != null, Setmeal::getName,name);
        qw.orderByDesc(Setmeal::getUpdateTime);

        setmealService.page(pageInfo,qw);
        BeanUtils.copyProperties(pageInfo,pageInfo2,"records");

        List<Setmeal> records = pageInfo.getRecords();
        ArrayList<SetmealDto> list = new ArrayList<>();

        for (Setmeal record : records) {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(record,setmealDto);
            Long id = record.getCategoryId();
            Category category = categoryService.getById(id);

            if (category != null) {
                String name1 = category.getName();
                setmealDto.setCategoryName(name1);
            }
            list.add(setmealDto);
        }

        pageInfo2.setRecords(list);

        return R.success(pageInfo2);
    }


    @DeleteMapping
    public R<String> delete(@RequestParam("ids") List<Long> ids){

        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        qw.in(Setmeal::getId,ids);
        qw.eq(Setmeal::getStatus,1);

        long count = setmealService.count(qw);
        if (count > 0){
            throw new CustomException("该套餐正在启售，不能删除!!");
        }

        setmealService.removeBatchByIds(ids);

        LambdaQueryWrapper<SetmealDish> qw1 = new LambdaQueryWrapper<>();
        qw1.in(SetmealDish::getSetmealId,ids);

        setmealDishService.remove(qw1);


        return R.success("套餐数据成功删除");
    }


    @PostMapping("/status/{status}")
    public R<List<Setmeal>> updateStatus(@PathVariable int status,Long[] ids){
        List<Setmeal> list = new ArrayList<>();
        for (Long id : ids) {
            Setmeal setmeal = setmealService.getById(id);
            setmeal.setStatus(status);
            list.add(setmeal);
        }
        setmealService.updateBatchById(list);
        return R.success(list);
    }

    @GetMapping("{id}")
    public R<Setmeal> getById(@PathVariable Long id){

        SetmealDto setmealDto = setmealService.getByIdWithCategory(id);

        return R.success(setmealDto);
    }

    @PutMapping
    public R<String> update(@RequestBody SetmealDto setmealDto){
        setmealService.updateById(setmealDto);

        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishService.updateBatchById(setmealDishes);

        Category category = categoryService.getById(setmealDto.getCategoryId());
        categoryService.updateById(category);

        return R.success("修改成功");
    }

    @GetMapping("/list")
    public R<List<Setmeal>> list(Long categoryId,int status){

        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        qw.eq(Setmeal::getCategoryId,categoryId);
        qw.eq(Setmeal::getStatus,status);
        qw.orderByDesc(Setmeal::getUpdateTime);

        List<Setmeal> list = setmealService.list(qw);
        if (list.isEmpty()){
            Category category = categoryService.getById(categoryId);
            String name = category.getName();
            log.warn("套餐->{} 暂时没有信息！ ",name);
        }

        return R.success(list);


    }


}
